package org.gjy.m8.concurrent;

import com.google.common.collect.Lists;
import com.googlecode.aviator.AviatorEvaluator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-06-30 14:41:58
 */
public class CountDownLatchTest {
    private static final Logger log = LoggerFactory.getLogger(CountDownLatchTest.class);

    public static void main(String[] args) {
        List<String> list = Lists.newArrayList("1+1", "99*12+122-12", "883-21", "90/25");
        CopyOnWriteArrayList<Object> result = new CopyOnWriteArrayList<>();
        CountDownLatch cdl = new CountDownLatch(list.size());
        ThreadPoolExecutor pool = ThreadPoolUtil.getDefaultPool();
        try {
            for (String s : list) {
                pool.submit(() -> {
                    Object execute = AviatorEvaluator.compile(s).execute();
                    result.add(execute);
                    log.info("算费: {}={}", s, execute);
                    cdl.countDown();
                });
            }

            cdl.await();
            System.out.println(result);
            ThreadPoolUtil.shutdown(pool);
            System.out.println("算费完成");
        } catch (InterruptedException e) {
            log.error("算费失败: {}", e.getMessage());
            throw new RuntimeException(e);
        }

    }

}
